package o19_10_11;

import org.apache.commons.lang3.time.DateFormatUtils;

public class MyTime {

    public long start = 0;
    public long temp = 0;
    public long naStart = 0;
    public long naTemp = 0;
    String pattern = "mm:ss.SSS";
    String name = "计时器:";

    public MyTime() {
        temp = start = System.currentTimeMillis();
        naTemp = naStart = System.nanoTime();
    }

    public MyTime(String name) {
        this.name = name;
        temp = start = System.currentTimeMillis();
        naTemp = naStart = System.nanoTime();
    }

    public MyTime(String name, String pattern) {
        this.pattern = pattern;
        temp = start = System.currentTimeMillis();
        naTemp = naStart = System.nanoTime();
    }

    public static void time (Runnable run) {
        System.out.println("------start----------------");
        MyTime myTime = new MyTime();
        run.run();
        myTime.end();
        System.out.println("------end--------");
    }

    public static void naTime (Runnable run) {
        System.out.println("------start----------------");
        MyTime myTime = new MyTime();
        run.run();
        myTime.endNa();
        System.out.println("------end--------");
    }


    public long pre() {
        long now = System.currentTimeMillis();
        long val = now - temp;
        temp = now;
        System.out.println(name + DateFormatUtils.format(val, pattern));
        return val;
    }


    public long end() {
        long now = System.currentTimeMillis();
        long val = now - start;
        System.out.println(name + DateFormatUtils.format(val, pattern));
        return val;
    }


    public long preNa() {
        long now = System.nanoTime();
        long val = now - naTemp;
        naTemp = now;
        System.out.println(name + val);
        return val;
    }


    public long endNa() {
        long now = System.nanoTime();
        long val = now - naStart;
        System.out.println(name + val);
        return val;
    }

}
